{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.signal import firwin, freqz\n",
    "import pylab as plt\n",
    "from utils import *\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "PHASE = 1\n",
    "TAPS = 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Design a filter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "taps = firwin(10, 0.8, window=\"blackman\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.plot(taps)\n",
    "plt.grid()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "w, h = freqz(taps, worN=8000)\n",
    "x = (w/np.pi)*PHASE\n",
    "y = to_db(np.absolute(h))\n",
    "max_image = max(y[int(8000/(PHASE/2)-1):])\n",
    "\n",
    "plt.plot(x, y, linewidth=2)\n",
    "plt.xlabel('Frequency (nyq)')\n",
    "plt.ylabel('Gain')\n",
    "plt.title('Frequency Response')\n",
    "#plt.xlim(0, 2)\n",
    "plt.ylim(-100, 0)\n",
    "plt.grid(True)\n",
    "plt.axhline(y=max_image, color='red', ls='--')\n",
    "plt.fill_between([0, 1], [-100, -100], alpha=0.2, color='green')\n",
    "plt.show()\n",
    "\n",
    "print(\"Maximum aliasing image %d dB\" % max_image)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Filter coefficients"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print_output(taps, PHASE)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
